﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SmsSetting.aspx.cs" Inherits="CRMWeb.Pages.Admin.SmsSetting" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>SMS Account Setting </title>
</head>
<body>
    <table id="dgSmsAcc" title="Sms Account Setting" style="width: 600%; height: 380px; overflow-y: auto;"
        toolbar="#toolbarSetting" pagination="true" data-options="pageSize:10,pageList: [10,20,30,50]" rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true">
        <thead>
            <tr>
                <th field="Company" width="200" align="center">Company</th>
                <th field="SMSAccount" width="500px" align="center">SMS Account</th>
                <th field="IsDefault" width="80" formatter="formatCheckbox" align="center">Default</th>
            </tr>
        </thead>
    </table>
    <div id="toolbarSetting">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newSetting()">New</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editSetting()">Edit</a>
        <a id="lnkDelete" href="javascript:void(0)" class="easyui-linkbutton" onclick="openDeletePopup()" plain="true" iconcls="icon-remove" title="Delete Sms Account Setting">Delete</a>
    </div>
    <div id="SmsdlgSettig" class="easyui-dialog" style="width: 420px; height: 180px; padding: 10px"
        closed="true" buttons="#dlg-buttons">
        <form id="fmdlgSetting" method="post" runat="server" novalidate>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Company:</label>
                <input class="easyui-combobox" id="cmbSmsCompany" style="width: 267px;" required="required"
                    data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'MemberGroupName',  
                        textField: 'MemberGroupName',
                        url: '/WebServices/Notice.ashx?method=BindAllRole',
                    selected:true" />
            </div>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Sms Account:</label>
                <input class="easyui-combobox" id="cmbSmsAcc" style="width: 267px;" required="required" data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'SmsID',  
                        textField: 'SmsID',
                        url: '/WebServices/SmsAccountSetting.ashx?method=BindSmsAccounts',
                    selected:true" />
            </div>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Default Account:</label>
                <input type="checkbox" id="is_active" name="is_active" value="true" />
            </div>
            <input id="hdnID" value="" style="display: none" />
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#SmsdlgSettig').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveSetting()">Save</a>
    </div>

    <script type="text/javascript">

        function newSetting() {
            $('#cmbSmsAcc').combobox('textbox').bind('click', function () {
                $('#cmbSmsAcc').combobox('showPanel');
            });
            $("#cmbSmsCompany").combobox('reload');
            $('#cmbSmsCompany').combobox({
                onLoadSuccess: function (data) {
                    $(this).combobox('select', data[0]['MemberGroupName']);
                    $('#cmbSmsCompany').combobox('textbox').bind('click', function () {
                        $('#cmbSmsCompany').combobox('showPanel');
                    });
                },
            });
            $('#SmsdlgSettig').dialog('open').dialog('setTitle', 'New Sms Account Setting');
            $('#fmdlgSetting').form('clear');
        }

        //Resize datagrid:
        window.onresize = function () {
            $('#dgSmsAcc').datagrid('resize', {
                width: window.innerWidth - 165,
                height: window.innerHeight - 40
            });
        }
        function editSetting() {
            $('#cmbSmsCompany').combobox('textbox').bind('click', function () {
                $('#cmbSmsCompany').combobox('showPanel');
            });

            $('#cmbSmsAcc').combobox('textbox').bind('click', function () {
                $('#cmbSmsAcc').combobox('showPanel');
            });
            var row = $('#dgSmsAcc').datagrid('getSelected');

            if (row) {
                $('#SmsdlgSettig').dialog('open').dialog('setTitle', 'Edit Sms Account Setting');
                $('#fmdlgSetting').form('load', row);
                $('#hdnID').val(row.ID);
                $('#cmbSmsCompany').combobox('setValue', row.Company);
                $('#cmbSmsAcc').combobox('setValue', row.SmsAccount);

                if (row.IsDefault == "1") {
                    $('#is_active').prop('checked', true);
                }
                else {
                    $('#is_active').prop('checked', false);
                }
            }
        }

        function openDeletePopup() {
            var row = $('#dgSmsAcc').datagrid('getSelected');
            if (row) {
                $('#headertext').html("Are you sure want to delete this SMS setting?");
                $('#dlgDelconfirm').dialog('open');
                $('#btnDelete').bind("click", function () {
                    deleteSetting();
                });
            }
        }

        function deleteSetting() {
            var row = $('#dgSmsAcc').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/SmsAccountSetting.ashx?method=DeleteSettings',
                    data: { ID: row.ID },
                    success: function (data) {
                        $('#dgSmsAcc').datagrid('reload');
                        $('#dlgDelconfirm').dialog('close');
                    }
                });
            }
        }


        $(document).ready(function () {
            $('#dgSmsAcc').datagrid({
                url: '/WebServices/SmsAccountSetting.ashx?method=GetSmsAccountData',
                onDblClickRow: function (index, rowdata) {
                    editSetting();
                }
            });
        });

        function formatCheckbox(value, row, index) {
            if (value == 1) {
                return "<input type='checkbox' checked=true  onclick='return false'>";
            }
        }

        function saveSetting() {
            var Company = $('#cmbSmsCompany').combobox('getValue');
            var SmsAccount = $('#cmbSmsAcc').combobox('getValue');
            var IsDefault = $('#is_active').attr('checked') ? 1 : 0;
            var ID = $('#hdnID').val();
            if (SmsAccount == '') {
                $.messager.alert('Morgan CRM', 'Please select SMS ID...!', 'warning');
            }
            else {

                if (ID != '') {
                    if (Company != '') {
                        $.ajax({
                            url: '/WebServices/SmsAccountSetting.ashx?method=EditSettings',
                            data: { 'ID': ID, 'Company': Company, 'SmsAccount': SmsAccount, 'IsDefault': IsDefault },
                            success: function (data) {

                                switch (data) {
                                    case "0":
                                        $.messager.alert('Morgan CRM', 'This account setting already exists.', 'info');
                                        break;
                                    case "1":
                                        $('#SmsdlgSettig').dialog('close');
                                        $('#dgSmsAcc').datagrid('reload');
                                        break;
                                    case "2":
                                        $.messager.alert('Morgan CRM', 'One account only have one default setting.', 'info');
                                        break;
                                }
                            }
                        });
                    }
                }
                else {
                    $.ajax({
                        url: '/WebServices/SmsAccountSetting.ashx?method=AddSettings',
                        data: { Company: Company, SmsAccount: SmsAccount, IsDefault: IsDefault },
                        success: function (data) {
                            switch (data) {
                                case "0":
                                    $.messager.alert('Morgan CRM', 'This account setting already exists.', 'info');
                                    break;
                                case "1":
                                    $('#SmsdlgSettig').dialog('close');
                                    $('#dgSmsAcc').datagrid('reload');
                                    break;
                                case "2":
                                    $.messager.alert('Morgan CRM', 'One account only have one default setting.', 'info');
                                    break;
                            }
                        }
                    });

                }
            }
        }
    </script>
</body>
</html>

